package com.taomu.app.zongchouhuli.http.api;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.taomu.app.zongchouhuli.BuildConfig;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;

/**
 * keZhong
 * 2018/9/22 14:08
 */
public class NetWork {

    public static String BASE_URL = BuildConfig.BASE_URL;

    public static String BASE_UPLOAD_URL = BuildConfig.BASE_URL;

    private static HttpLoggingInterceptor loggingInterceptor;

    private static Interceptor logInterceptor;

    private static ApiService apiService;

    public static ApiService getApi() {
        if(apiService == null){
            apiService = getUserRequestService(ApiService.class);
        }
        return apiService;
    }

    /**
     * 获取用户api请求service
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getUserRequestService(Class<T> clazz) {
        return getBaseRefrofit().create(clazz);
    }

    public static UploadService getUploadApiService() {
        return getUploadRequestService(UploadService.class);
    }

    /**
     * 上传文件api请求service
     *
     * @return
     */
    public static <T> T getUploadRequestService(Class<T> clazz) {
        return getUploadRetrofit().create(clazz);
    }

    private static Retrofit getBaseRefrofit() {
        return new RetrofitBuilder(BASE_URL)
                .setInterceptor(getLoggingInterceptor())
                .create();
    }

    private static Retrofit getUploadRetrofit(){
        return new RetrofitBuilder(BASE_UPLOAD_URL)
                .setInterceptor(getLoggingInterceptor())
                .create();
    }



    private static HttpLoggingInterceptor getLoggingInterceptor() {
        if (loggingInterceptor == null) {
            //设置网络请求日志打印级别
            HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
            loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                @Override
                public void log(String s) {
                    Log.d("okHttp_message", s);
                }
            });
            loggingInterceptor.setLevel(level);
        }
        return loggingInterceptor;
    }

    /**
     * 获取打印请求的interceptor
     * @return
     */
    private static Interceptor getLogInterceptor() {
        if (logInterceptor == null) {
            logInterceptor = new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    okhttp3.Request request = chain.request();
                    Log.i("okHttp_request_headers", JSON.toJSONString(request.headers()));
                    Log.i("okHttp_request_body", JSON.toJSONString(request.body()));
                    Response response = chain.proceed(request);
                    Log.i("okHttp_response_body", response.body().string());
                    return response;
                }
            };
        }
        return logInterceptor;
    }


}
